package com.domino.client;


import java.util.Date;
import java.util.List;

import com.domino.client.view.BoardGamePanel;
import com.domino.shared.SessionStateResponseFinished;
import com.domino.shared.model.Color;
import com.domino.shared.model.SimpleBoard;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Minimal administration page functionality
 */
public class Admin implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";


	private final AdminServiceAsync adminService =  GWT.create(AdminService.class);
	
	HTML html = new HTML();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		RootPanel.get("sessions").add(html);
		
		Date from = oneWeekAgo();
		Date to = oneMinuteAhead();
		html.setText("gurka");
		
		adminService.getSessionInfo(from, to, new AsyncCallback<List<String>>() {
			
			@Override
			public void onSuccess(List<String> result) {
				String sessionInfo = "<br><b>Result set count: " + result.size() + "</b><hr><hr>";
				for(String s : result) {
					sessionInfo += s + "<hr>";
				}
				html.setHTML(sessionInfo);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				html.setText(SERVER_ERROR + "<hr>" + caught.getLocalizedMessage());
			}
		});

	}

	private Date oneWeekAgo() {
		return new Date(System.currentTimeMillis()-7*24*3600*100);
	}

	private Date oneMinuteAhead() {
		return new Date(System.currentTimeMillis()+60*1000);
	}
}
